<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %><%--
本页面仅限管理员进入，向一个投票中添加新的选项,仅负责后端处理,不负责显示页面
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    request.setCharacterEncoding("UTF-8");
    //从session获取投票页面的id
    int voteId = Integer.parseInt(session.getAttribute("voteId").toString());
    //从表单中获取数据
    String option = request.getParameter("option");
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai", "root", "123456");
        PreparedStatement preparedStatement = connection.prepareStatement("insert into vote_option(vote_id,`option`) values (?,?)");
        preparedStatement.setInt(1, voteId);
        preparedStatement.setString(2, option);
        if (preparedStatement.executeUpdate() > 0) {
            //打印到控制台
            System.out.println("保存成功!");
        } else {
            System.out.println("保存失败!");
        }
        preparedStatement.close();
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
        out.print(e.getMessage());
    }
    //带着Vote的id返回上一个也页面
    response.sendRedirect("../adminVoteOption.jsp?id=" + voteId);
%>
</body>
</html>
